<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 前台
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-03-03 09:31:20 +0800 (一, 2014-03-03) $
 * @version : $Id: home_model.php 39 2014-03-03 01:31:20Z spike $
 * @rev     : $Revision: 39 $
 */
class Home_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * [新增评论]
     * @param  [array]  $params [所有信息的数组]
     * @return [bool]
     */
    public function add_comment($params)
    {
        $params = array(
            'nickname' => $params['nickname']  ? clean_html(xss_clean($params['nickname'])) : NULL,
            'comment'  => $params['comment']   ? clean_html(xss_clean($params['comment']))  : NULL,
        );
        
        // 校验中文名
        if( ! validate_length($params['nickname'], 1, 50) )
        {
            $this->set_error("新增评论失败：昵称不能为空，最多50个字。");
            return FALSE;
        }
        $data['nickname'] = $params['nickname'];

        // 校验英文名
        if( ! validate_length($params['comment'], 1, 200) )
        {
            $this->set_error("新增评论失败：评论内容不能为空，最多200个字。");
            return FALSE;
        }
        $data['comment'] = $params['comment'];

        // 写入
        $data['status']     = COMMENT_STATUS_WAIT;
        $data['ip_address'] = $this->input->ip_address();
        $data['is_delete']  = UNDELETED;
        $data['create_time']= now_str();

        $this->load->dao('Comment_dao');
        if( ! $this->Comment_dao->insert($data))
        {
            $this->set_error("新增评论失败：无法写入数据库。");
            return FALSE;
        }

        return TRUE;
    }

    // --------------------------------------------------------------------
}